package winTime;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import winTime.process.DisplayTick;
import winTime.process.Worker;
import winTime.process.Write2File;
import winTime.process.source.FutFileHandler;
import winTime.process.source.ipushFileHandler;
import winTime.vo.TickKLine;
/**
 * @author fatsung
 * 產生K線,以秒為主
 * 大台分佈, O,H,L,C,qty,count
 */
/**
 * @author Administrator
 *
 */
public class Start {
	private static final Logger log=LoggerFactory.getLogger(Start.class);
	private static final String defaultIni="ini/secondK.ini";
	private static File iniFile;
	private static Properties ini=new Properties();
	private static LinkedBlockingQueue<Object> queue=new LinkedBlockingQueue<Object>();
	public static List<TickKLine> td = new ArrayList<TickKLine>();//存放當日K線資料
	private static final List<Thread> active=new ArrayList<Thread>();
	public static final String dateTag="2013_02_18";
	public static final String smonth="201302";
	/**
	 * @程式進入點
	 */
	public static void main(String[] args){
		log.info("start");
		if(args.length>0){
			iniFile=new File(args[0]);
		}else{
			iniFile=new File(defaultIni);
		}
		try{
			setEnv();
			startThreads();
			monitor();
		}catch(Exception e){
			log.error("Error:{}",e);
		}
		log.info("end");
	}

	/**
	 * 依序啟動Worker Thread, Ipush Thread及Jms Thread.
	 *
	 */
	private static void startThreads() {
		startWorker();
		startFileRpt();
		startWrite2File();
//		startDisplayTick();
//		startIpush();
	}
	/**
	 * 將秒K資料寫到檔案中
	 */
	private static void startWrite2File(){
		log.info("Start Write2File thread");
		Write2File wf = new Write2File();
		wf.start();
		active.add(wf);
	}
	/**
	 * 顯示計算後的秒K資料
	 */
	private static void startDisplayTick(){
		log.info("Start Tick Display thread");
		DisplayTick tick = new DisplayTick();
		tick.start();
		active.add(tick);
	}
	private static void startFileRpt(){
		log.trace("Start FutFileHandler thread!");
		FutFileHandler fh = new FutFileHandler();
		fh.start();
		active.add(fh);
	}	
	/**
	 * 啟動File Thread
	 */
	private static void startFile(){
		log.trace("Start ipushFileHandler thread!");
		ipushFileHandler fh = new ipushFileHandler();
		fh.start();
		active.add(fh);
	}
	/**
	 * 啟動Worker Thread.
	 *
	 */
	private static void startWorker(){
		log.info("Start Worker thread!");
		Worker worker=new Worker();
		worker.start();
		active.add(worker);
	}
	/**
	 * 設定初始環境,讀取設定檔
	 */
	private static void setEnv(){


	}
	/**
	 * 將物件加入到FIFO queue裡等待Worker處理.
	 *
	 * @param obj
	 */
	public static void put(Object obj){
		synchronized (queue) {
			if(queue.add(obj)){
				queue.notify();
			}
		}
	}
	/**
	 * 從FIFO queue取得物件.
	 *
	 * @return
	 * @throws InterruptedException
	 */
	public static Object poll() throws InterruptedException{
		synchronized (queue) {
			if(queue.isEmpty()){
				queue.wait();
			}
			return queue.poll();
		}
	}
	/**
	 * TODO write the document
	 *
	 * @throws InterruptedException
	 */
	private static void monitor() throws InterruptedException {
		int wait=getIntProperty("MONITOR_WAIT_MS",15000);
		if(wait>0){
			while(true){
				Thread.sleep(wait);
				if(active.isEmpty()){
					log.debug("No active thread found! System exit.");
					break;
				}
				log.info("Active Threads: {}, Queue Count: {}",active.size(), queue.size());
			}
		}else{
			synchronized (active) {
				active.wait();
			}
		}
	}
	/**
	 * 取得設定檔裡key的值(int), 如果找不到對應的key時就回傳defaultI.
	 *
	 * @param key
	 * @param defaultI
	 * @return
	 */
	public static int getIntProperty(String key,int defaultI){
		return ini.getProperty(key)==null?defaultI:Integer.valueOf(ini.getProperty(key));
	}
}
